package zhang.automobileservice;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import bean.LoginBean;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import utils.MyUrl;
import utils.RequestQueueUtils;

import static zhang.automobileservice.R.color.discover;

public class RegisterActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private EditText et_putphonenum;
    private EditText et_putnumcheck;
    private Button ib_checkcode;
    private EditText et_putpwd;
    private ImageButton ib_visible;
    private ImageButton ib_invisible;
    private Button bt_submit;
    private String tel;
    private TimeCount time;
    private EventHandler eh  = new EventHandler(){
        @Override
        public void afterEvent(int event, int result, Object data) {
            Log.e("======", "event=" + event + "  result=" + result + "  data" + data.toString());
            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                    Log.e("======", "提交验证码成功");
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                    Log.e("+++++","获取验证码成功");
                    handler.sendEmptyMessage(1);

                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                ((Throwable) data).printStackTrace();
                Log.e("======", data.toString());
            }
        }
    };
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    ib_checkcode.setClickable(true);
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        SMSSDK.registerEventHandler(eh); //注册短信回调

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.finish:
                finish();
                break;
            case R.id.ib_checkcode:
                time = new TimeCount(30000, 1000);
                time.start();
                tel = et_putphonenum.getText().toString();
                if(tel.isEmpty()){
                    Toast.makeText(this,"请输入电话号码",Toast.LENGTH_LONG).show();
                }
                SMSSDK.getVerificationCode("86", tel);
                break;
            case R.id.bt_submit:
                SMSSDK.submitVerificationCode("86", tel, et_putnumcheck.getText().toString());
            /*{"code":0,"errorMessage":"","data":{"id":26,"userName":"12345678","password":"zwy11526","trueName":"","headPic":"default.jpg","sex":"",
                    "phone":"12345678","openPhone":0,"email":"","openEmail":0,"qq":"","openQQ":0,"weichart":"","openWeichart":0,"remark":"","type":0}}*/
                String url = MyUrl.Font_Url+MyUrl.register+"phone="+et_putphonenum.getText()+"&password="+et_putpwd.getText();
                RequestQueueUtils.exectueJsonRequest(this, url, new Response.Listener<JSONObject>() {
                        @Override
                    public void onResponse(JSONObject jsonObject) {
                            JSONObject obj;
                        try {
                            obj = jsonObject.getJSONObject("data");
                            if("0".equals(jsonObject.getString("code"))){
                            }
                            Log.e("-----",obj.toString());
                            Intent intent = new Intent(RegisterActivity.this,MyLoginActivity.class);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
    }

    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        et_putphonenum = (EditText) findViewById(R.id.et_putphonenum);
        et_putnumcheck = (EditText) findViewById(R.id.et_putnumcheck);
        ib_checkcode = (Button) findViewById(R.id.ib_checkcode);
        et_putpwd = (EditText) findViewById(R.id.et_putpwd);
        ib_visible = (ImageButton) findViewById(R.id.ib_visible);
        ib_invisible = (ImageButton) findViewById(R.id.ib_invisible);
        bt_submit = (Button) findViewById(R.id.bt_submit);

        finish.setOnClickListener(this);
        ib_checkcode.setOnClickListener(this);
        ib_visible.setOnClickListener(this);
        ib_invisible.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
    }



    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            ib_checkcode.setBackgroundColor(getResources().getColor(R.color.discover));
            ib_checkcode.setText("获取验证码");
            ib_checkcode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            ib_checkcode.setBackgroundColor(getResources().getColor(R.color.newsconsult));
            ib_checkcode.setClickable(false);//防止重复点击
            ib_checkcode.setText((millisUntilFinished / 1000) + "s");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eh);
    }
}
